package isv

import (
    "bytes"
    "wxsdk/miniapp/core"
    "dhfshop/models/isv"
	"dhfshop/controllers/base"
	"dhfshop/service/img/mark"
	"dhfshop/service/img/imgutil"
	"wxsdk/miniapp/qrcode"
	"encoding/base64"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type AcodeController struct {
	base.BaseController
}

// 获取小程序码，直达页面
func (this *AcodeController) GetWxAcode() {
    v := struct {
	    AuthorizerAppid string  `form:"authorizer_appid"`
		Path            string  `form:"path"`
		AutoColor       bool    `form:"auto_color"`
		R               string  `form:"r"`
		G               string  `form:"g"`
		B               string  `form:"b"`
		Base64          bool    `form:"base64"`
	}{Base64:false, AutoColor:true}     
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.AuthorizerAppid, "authorizer_appid")
	valid.Required(v.Path, "path")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	accessToken := ""
	if v.AuthorizerAppid == beego.AppConfig.String("SellerAppID"){
	    authorize, err := isv.GetAuthorizeByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	    accessToken = authorize.AccessToken
	}else{
	    custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
		accessToken = custAccount.AccessToken
	}
	data, err := qrcode.GetWxAcode(accessToken,v.Path,v.AutoColor,v.R,v.G,v.B)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if v.Base64 {
	    encodeString := base64.StdEncoding.EncodeToString(data)
	    this.Ctx.ResponseWriter.Write([]byte(encodeString))
	    return
	}
	this.Ctx.ResponseWriter.Write(data)
}

// 获取小程序码，直达首页
func (this *AcodeController) GetWxAcodeUnlimit() {
	v := struct {
	    AuthorizerAppid string  `form:"authorizer_appid"`
		Scene            string  `form:"scene"`
		Page            string  `form:"page"`
		AutoColor       bool    `form:"auto_color"`
		R               string  `form:"r"`
		G               string  `form:"g"`
		B               string  `form:"b"`
		Base64          bool    `form:"base64"`
		Width           int     `form:"width"`
	}{Base64:false, AutoColor:true, Width:430}
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.AuthorizerAppid, "authorizer_appid")
	valid.Required(v.Scene, "scene")
	valid.Required(v.Page, "page")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	accessToken := ""
	if v.AuthorizerAppid == beego.AppConfig.String("SellerAppID"){
	    authorize, err := isv.GetAuthorizeByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	    accessToken = authorize.AccessToken
	}else{
	    custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
		accessToken = custAccount.AccessToken
	}
	data, err := qrcode.GetWxAcodeUnlimit(accessToken,v.Scene,v.Page,v.AutoColor,v.R,v.G,v.B,v.Width)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if v.Base64 {
	    encodeString := base64.StdEncoding.EncodeToString(data)
	    this.Ctx.ResponseWriter.Write([]byte(encodeString))
	    return
	}
	this.Ctx.ResponseWriter.Write(data)
}

// 获取小程序二维码，直达页面
func (this *AcodeController) CreateWxAqrCode() {
    v := struct {
	    AuthorizerAppid string  `form:"authorizer_appid"`
		Path            string  `form:"path"`
		Base64          bool    `form:"base64"`
	}{Base64:false}
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.AuthorizerAppid, "authorizer_appid")
	valid.Required(v.Path, "path")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	accessToken := ""
	if v.AuthorizerAppid == beego.AppConfig.String("SellerAppID"){
	    authorize, err := isv.GetAuthorizeByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	    accessToken = authorize.AccessToken
	}else{
	    custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
		accessToken = custAccount.AccessToken
	}
	data, err := qrcode.CreateWxAqrCode(accessToken, v.Path)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if v.Base64 {
	    encodeString := base64.StdEncoding.EncodeToString(data)
	    this.Ctx.ResponseWriter.Write([]byte(encodeString))
	    return
	}
	this.Ctx.ResponseWriter.Write(data)
}

// 小程序码叠加到主图上
func (this *AcodeController) ImgOverlayAcodeUnlimit() {
	v := struct {
	    AuthorizerAppid string  `form:"authorizer_appid" valid:"Required"`
		ImageUrl        string  `form:"image_url" valid:"Required"`
		Scene           string  `form:"scene" valid:"Required"`
		Page            string  `form:"page" valid:"Required"`
		AutoColor       bool    `form:"auto_color"`
		R               string  `form:"r"`
		G               string  `form:"g"`
		B               string  `form:"b"`
		AcodeWidth      int     `form:"acode_width"`
	}{AutoColor:false,R:"0",G:"0",B:"0"}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	/*
	beego.Debug("v.AuthorizerAppid:", v.AuthorizerAppid)
	beego.Debug("v.ImageUrl:", v.ImageUrl)
	beego.Debug("v.Scene:", v.Scene)
	beego.Debug("v.Page:", v.Page)
	*/
	accessToken := ""
	if v.AuthorizerAppid == beego.AppConfig.String("SellerAppID"){
	    authorize, err := isv.GetAuthorizeByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	    accessToken = authorize.AccessToken
	}else{
	    custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
		accessToken = custAccount.AccessToken
	}
	byteData, err := core.GetCommonFileByHttp(v.ImageUrl)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	backgroundR := bytes.NewReader(byteData)
	height, err := imgutil.GetImageHeight(backgroundR, 1)  // 0:png, 1:jpg
	if err != nil {
	    beego.Debug("GetImageHeight err:", err.Error())
		this.ServeError(err.Error())
		return
	}
	dstHeight := height/3
	if dstHeight > 430 {
	    dstHeight = 430
	}else {
	
	}
	if v.AcodeWidth > 0 {
	    dstHeight = v.AcodeWidth
	}
	//beego.Debug("background img H:", height, "logo img H:",dstHeight)
	waterImg, err := qrcode.GetWxAcodeUnlimit(accessToken,v.Scene,v.Page,v.AutoColor,v.R,v.G,v.B,dstHeight)
	if err != nil {
	    beego.Debug("GetWxAcodeUnlimit err:", err.Error())
		this.ServeError(err.Error())
		return
	}
	backgroundR = bytes.NewReader(byteData)  //计算尺寸的时候，内存是否进行了释放?
	waterImgR := bytes.NewReader(waterImg)
	markWriter := mark.NewMark()
	err = markWriter.Mark(backgroundR, waterImgR, this.Ctx.ResponseWriter, 1)  // 0:png, 1:jpg
	if err != nil {
		beego.Debug("Mark err:", err.Error())
		this.ServeError(err.Error())
		return
	}
}


